<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class CourseSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'UserSeeder',
            'CategorySeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $categories = $this->fetchAll('SELECT * FROM kg_category');

        $teachers = $this->fetchAll('SELECT * FROM kg_user WHERE edu_role=2');

        $rows = [];
        for ($i = 0; $i < 300; $i++) {
            $rows[] = [
                'title' => $faker->text(50),
                'cover' => $faker->imageUrl(150, 80),
                'summary' => $faker->text(250),
                'keywords' => '',
                'details' => $faker->text(500),
                'category_id' => Arr::rand($categories)['id'],
                'teacher_id' => Arr::rand($teachers)['id'],
                'origin_price' => 0,
                'market_price' => 0,
                'vip_price' => 0,
                'study_expiry' => 12,
                'refund_expiry' => 30,
                'rating' => $faker->randomFloat(2, 4, 5),
                'score' => $faker->randomFloat(2, 0.1, 0.5),
                'model' => rand() % 9 === 0 ? 3 : 1,
                'level' => Arr::rand([1, 2, 3]),
                'attrs' => json_encode(['duration' => $faker->numberBetween(1000, 20000), 'word_count' => $faker->numberBetween(1000, 20000)]),
                'featured' => rand() % 23 === 0 ? 1 : 0,
                'published' => 1,
                'user_count' => rand(1000, 10000),
                'lesson_count' => rand(0, 500),
                'review_count' => rand(0, 100),
                'favorite_count' => rand(0, 10000),
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_course')->insert($rows)->saveData();
    }

}
